package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.OrderInfo;

import java.util.List;

/**
 * Created by Jimbo on 2015/10/11.
 */
public class PersonalOrderListAdapter extends BaseAdapter {
    private Context mContext;
    private List<OrderInfo> data;
    private View.OnClickListener listener;
    public PersonalOrderListAdapter(Context context, List<OrderInfo> data,View.OnClickListener listener){
        mContext = context;
        this.data = data;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_message_order_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        OrderInfo orderInfo = data.get(position);
        holder.tv_id.setText(orderInfo.getPay_sn());
        List<OrderInfo.MyOrderItem> order_list = orderInfo.getOrder_list();
        OrderInfo.MyOrderItem myOrderItem = order_list.get(0);
        List<OrderInfo.MyOrderItem.ExtendOrderGoods> eog = myOrderItem.getExtend_order_goods();
        OrderInfo.MyOrderItem.ExtendOrderGoods extendOrderGoods = eog.get(0);
        if(eog.size() > 1) {
            holder.tv_name.setText(extendOrderGoods.getGoods_name() + " 等" + eog.size() + "件");
        }else{
            holder.tv_name.setText(extendOrderGoods.getGoods_name());
        }
        holder.tv_type.setText(myOrderItem.getPayment_name());
        holder.tv_state.setText(myOrderItem.getState_desc());

        holder.tv_confirm.setTag(position);
        holder.tv_confirm.setOnClickListener(listener);
        holder.tv_connect.setOnClickListener(listener);
        return convertView;
    }

    public void setData(List<OrderInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView tv_name;
        TextView tv_id;
        TextView tv_type;
        TextView tv_state;
        TextView tv_other;
        TextView tv_connect;
        TextView tv_confirm;

        public ViewHolder(View view){
            tv_name = (TextView) view.findViewById(R.id.message_order_name);
            tv_id = (TextView) view.findViewById(R.id.message_order_id);
            tv_type = (TextView) view.findViewById(R.id.message_order_type);
            tv_state = (TextView) view.findViewById(R.id.message_order_state);
            tv_other = (TextView) view.findViewById(R.id.message_order_other);
            tv_connect = (TextView) view.findViewById(R.id.message_order_connect);
            tv_confirm = (TextView) view.findViewById(R.id.message_order_confirm);
        }
    }
}
